var app = getApp()
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: null,
    user: {},
    content: null,
    adr: null,
    adr_name: null,
    date: util.converDate(new Date()),
    start: util.converDate(new Date()),
    end: util.converDate(new Date(2032, 12, 31)),
    start_time: util.converTime(new Date()),
    end_time: util.converTime(new Date()),
    latitude: 0,
    longitude: 0,
    add_field: false,
    kinds: [],
    f: "",
    imageList: []
  },

  bindAddress: function (evt) {
    var that = this
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          adr: res.address,
          adr_name: res.name,
          latitude: res.latitude,
          longitude: res.longitude
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    app.getUserInfo(function (userInfo) {
      that.setData({
        user: userInfo
      })
    })

  },
  bindDateChange(e) {
    this.setData({
      date: e.detail.value.replace(/-/g, "/")
    })
  },
  bindStartTimeChange(e) {
    this.setData({
      start_time: e.detail.value,
      end_time: e.detail.value
    })
  },
  bindEndTimeChange(e) {
    this.setData({
      end_time: e.detail.value
    })
  },

  bindTitle(e) {
    this.setData({
      title: e.detail.value
    })
  },
  bindText(e) {
    this.setData({
      content: e.detail.value
    })
  },
  addField(e) {
    if (e.detail.value.name === null || !e.detail.value.name) {
      return false
    }
    var kinds = this.data.kinds;
    if (kinds.includes(e.detail.value.name)) {
      return false
    }
    if (kinds.length > 2) {
      wx.showModal({
        title: '事项太过多了, 只能添加三项',
      })
      return false
    }

    kinds.push(e.detail.value.name)
    this.setData({
      f: "",
      kinds: kinds
    })

  },

  deleteItem(e) {
    var d = this.data.kinds
    var idx = e.currentTarget.dataset.item
    d.splice(idx, 1)
    this.setData({
      kinds: d
    })
  },
  fieldChange(e) {
    this.setData({
      add_field: e.detail.value,
      kinds: []
    })
  },
  createSubject(e) {
    if (this.data.content && this.data.content.length > 199) {
      wx.showToast({
        title: '备注文字超过限制',
      })
      return false
    }
    if (this.data.title && this.data.title.length > 39){
      wx.showToast({
        title: '标题文字超过限制',
      })
      return false
    }
    var args = {
      title: this.data.title,
      content: this.data.content,
      address: this.data.adr,
      status: this.data.adr_name,
      lat: this.data.latitude,
      lon: this.data.longitude
    }
    if (this.data.add_field && this.data.kinds.length > 0) {
      var f = this.data.kinds.map(function (item) {
        return { title: item }
      })
      args.kind = f
    }
    if (this.data.imageList.length > 0) {
      args.pictures = this.data.imageList.map(function (val) {
        return { image: val }
      })
    }
    args.start_time = util.dateStringToInt(this.data.date + " " + this.data.start_time + ":00")
    args.deadline = util.dateStringToInt(this.data.date + " " + this.data.end_time + ":00")

    wx.request({
      url: "https://kaifan.chuanggj.com/wechat/subjects",
      method: "POST",
      data: args,
      header: {
        "Authorization": wx.getStorageSync("token")
      },
      success(e) {
        if (e.data.code > 200) {
          wx.showModal({
            title: '错误提示',
            content: e.data.msg,
          })
        } else {
          wx.redirectTo({
            url: '../subject/index?id=' + e.data.subject.id + "&title=" + e.data.subject.title
          })
        }
      }
    })
  },
  //选择图片
  uploadImage(event) {
    var that = this
    wx.chooseImage({
      count: 9,
      success(arg) {
        var lists = arg.tempFilePaths
        for (var list in lists) {
          that.pushImage(lists[list])
        }
      }
    })
  },
  showImage(event) {
    var that = this
    var current = event.currentTarget.dataset.src
    wx.previewImage({
      current: current,
      urls: that.data.imageList,
      success(arg) {
        console.log(arg)
      },
      fail(err) {
        console.log(err)
      }
    })
  },
  pushImage(img_url) {
    var that = this
    wx.uploadFile({
      url: 'https://kaifan.chuanggj.com/wechat/upload',
      filePath: img_url,
      name: 'file',
      header: {
        "Authorization": wx.getStorageSync("token")
      },
      success(res) {
        try {
          console.log(res.data)
          var url = JSON.parse(res.data)
          var list = that.data.imageList
          if (list.length >= 9) {
            wx.showToast({
              title: '图片数量超过限制',
            })
            return false
          }
          list.push(url.url)
          that.setData({
            imageList: list
          })
        }
        catch (err) {
          wx.showToast({
            title: '有一张图片出现不可修复的错误',
          })
        }
      },
      fail(err) {
        wx.showToast({
          title: '上传失败了, 请重新上传',
        })
      }
    })
  },


})
